var express = require('express');
var formidable = require('formidable');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var exec = require('child_process').exec;
var sd = require('silly-datetime');



// function runTest() {
// 	var time=sd.format(new Date(), 'YYYY-MM-DDHH:mm');
// 	var cli = "cd /Users/Ru/Desktop/videopodDemo/qiyivideo2;xcodebuild -workspace QiYiVideo.xcworkspace -scheme 'iQiYiPhoneVideoUITests' -sdk iphoneos10.2 -destination 'platform=iOS,name=test' test| xcpretty --report html --output /Users/Ru/Desktop/videopodDemo/testServer/uitestLog/"+time+"report.html;";
// 	console.log(cli);
// 	exec(cli,{encoding:'utf8'},function (err,stdout,stderr){
// 	    if (err){
// 	        console.log(err);
// 	        return;
// 	    }
// 	    console.log('stdout'+stdout);
// 	    console.log('stderr'+stderr);
// 	    runTest();
// 	})
// }





// function runTest2() {
// 	var time=sd.format(new Date(), 'YYYY-MM-DDHH:mm');
// 	var cli = "cd /Users/Ru/Desktop/videopodDemo/qiyivideo2;xcodebuild -workspace QiYiVideo.xcworkspace -scheme 'iQiYiPhoneVideoUITests' -sdk iphoneos10.2 -destination 'platform=iOS,name=xieru' test| xcpretty --report html --output /Users/Ru/Desktop/videopodDemo/testServer/uitestLog/"+time+"report.html";
// 	console.log(cli);
// 	exec(cli,{encoding:'utf8'},function (err,stdout,stderr){
// 	    if (err){
// 	        console.log(err);
// 	        return;
// 	    }
// 	    console.log('stdout'+stdout);
// 	    console.log('stderr'+stderr);
// 	    runTest2();
// 	})
// }
// cli = "xcodebuild -workspace QiYiVideo.xcworkspace -scheme 'iQiYiPhoneVideoUITests' -sdk iphoneos10.2 -destination 'platform=iOS,name=xieru' test| xcpretty --report html --output /Users/Ru/Desktop/videopodDemo/testServer/uitestLog/report.html";
// exec(cli,{encoding:'utf8'},function (err,stdout,stderr){
//     if (err){
//         console.log(err);
//         return;
//     }
//     console.log('stdout'+stdout);
//     console.log('stderr'+stderr);
// })

// runTest2();

// runTest2();
app.get('/',function (req,res) {

	res.send('testServer is running');
})

app.post('/crashlog',function (req,res) {
	var form = new formidable.IncomingForm();

	form.uploadDir = "./crashFiles";
    form.parse(req, function (err, fields, files) {
        //得到表单之后做的事情
        // console.log(fields);
        console.log(files.crashlog.name);

        var oldPath = files.crashlog.path;
        var newPath = "crashFiles/" + files.crashlog.name;
        fs.renameSync(oldPath,newPath);

        sendCrashlog(newPath,files.crashlog.name);

        res.statusCode=200;
        res.end(); 

    });


})


app.post('/uitestLog',function (req,res) {
	var form = new formidable.IncomingForm();

	form.uploadDir = "./uitestLog";
    form.parse(req, function (err, fields, files) {
        //得到表单之后做的事情
        console.log(fields);
        // console.log(files.uitestlog.name);

        // var oldPath = files.uitestlog.path;
        // var newPath = "uitestLog/" + files.uitestlog.name;
        // fs.renameSync(oldPath,newPath);



        // sendCrashlog(newPath,files.uitestlog.name);

        res.statusCode=200;
        res.end(); 



    });


})

function sendCrashlog(crashlogPath,crashlogName) {

	var smtpTransport = nodemailer.createTransport({
		host : 'smtp.qq.com',
		secureConnection: true, // 使用SSL方式（安全方式，防止被窃取信息）
		auth : {
			user : '120148341@qq.com',
			pass : 'ztwujzebtquxbhej'
		},
	});

	console.log(__dirname + '/' + crashlogPath);
	

		// 设置邮件内容
	var mailOptions = {
	  from: "QXtest<120148341@qq.com>", // 发件地址
	  // to: "xieru_sx@qiyi.com, louisliu@qiyi.com", // 收件列表
	  to: "xieru_sx@qiyi.com,120148341@qq.com",
	  subject: "crashlog", // 标题
	  html: "<b>thanks a for visiting!</b> 世界，你好！", // html 内容
	  attachments:[
		  {
			  filename: crashlogName,
			  path: __dirname + '/' + crashlogPath
		  },
	    ]
	}

	smtpTransport.sendMail(mailOptions, function (err, info) {  
    if (err) {  
      console.log(err);  
      return;  
    }  

    console.log(info);
    console.log('发送成功');  
  });

}


app.listen(3001);